package pl.soulmusic.phylactery.soap;

import java.util.ArrayList;

import pl.soulmusic.phylactery.db.DAO;
import pl.soulmusic.phylactery.map.Song;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;

public class SendSongsTask extends AsyncTask<String, Integer, String> {
	private Context ctx = null;

	public SendSongsTask(Context ctx) {
		this.ctx = ctx;
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected String doInBackground(String... params) {
		DAO dao = new DAO(ctx, null);
		ArrayList<Song> list = dao.getLoggedTracks();
		String result = "";
		TelephonyManager telephonyManager = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		String IMEI = telephonyManager.getDeviceId();
		for (int i = 0; i < list.size(); i++) {
			Song song = list.get(i);
			// TODO: change to IMEI!
			result = ServiceInvoker.addTrack("user", "1216985755", (double)song
					.getGeoPoint().getLongitudeE6() / 1000000, (double)song.getGeoPoint()
					.getLatitudeE6() / 1000000, "unknown", song.getAlbum(), "", song
					.getArtist(), "", song.getTitle(), "unknown");
			if (!result.equals(ServiceInvoker.SOAP_ACTION_OK))
				break;
		}
		return result;
	}

	@Override
	protected void onProgressUpdate(Integer... progress) {

	}

	protected void onPostExecute(String result) {
		AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
		alertbox.setMessage(result);
		alertbox.setNegativeButton("OK", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		alertbox.show();
	}

}
